// 安装并引入相关模块
import { mapGetters } from 'vuex'
import _ from 'lodash'
export default {
  data () {
    return {
      dataList: [],
      socketList: [],
      requestId: '1111', // 唯一的userId用于连接盒子，此id必须唯一
      deviceId: '', // 当前订阅的boxId
      programId: '', // 传输文件，当前订阅的requestId
      programId1: '' // 传输文件，当前订阅的requestId
    }
  },
  computed: {
    ...mapGetters([
      'client'
    ])
  },
  mounted () {
    // this.requestId = 'webClient' + this.userInfo.user_id.toString() + Date.parse(new Date()).toString()
    this.requestId = this.getRandom()
  },
  beforeDestroy () {
    // 取消订阅
    const subList = this.socketList
    console.log(subList)
    if (!_.isEmpty(subList)) {
      subList.map(v => this.client.unsubscribe(v))
    }
    this.socketList = []
  },
  methods: {
    // 获取随机数 默认1-2147483647
    getRandom (min = 1, max = 2147483647) {
      min = Math.ceil(min)
      max = Math.floor(max)
      return Math.floor(Math.random() * (max - min + 1)) + min
    },
    onSubscribe (deviceId, onMessageArrived) {
      if (!this.deviceId || this.deviceId.length === 0) {
        this.deviceId = deviceId
        // 订阅主题
        this.client.subscribe(
          `${this.CommonConfig.socketConfig.SOCKET_TOPIC.machineFileInfo.subscribeTopic}${this.deviceId}`,
          onMessageArrived,
          { id: `${this.CommonConfig.socketConfig.SOCKET_TOPIC.machineFileInfo.subscribeTopic}${this.deviceId}` }
        )
        this.socketList.push(`${this.CommonConfig.socketConfig.SOCKET_TOPIC.machineFileInfo.subscribeTopic}${this.deviceId}`)
      } else {
        this.deviceId = deviceId
        // 订阅主题
        this.client.subscribe(
          `${this.CommonConfig.socketConfig.SOCKET_TOPIC.machineFileInfo.subscribeTopic}${this.deviceId}`,
          onMessageArrived,
          { id: `${this.CommonConfig.socketConfig.SOCKET_TOPIC.machineFileInfo.subscribeTopic}${this.deviceId}` }
        )
        this.socketList.push(`${this.CommonConfig.socketConfig.SOCKET_TOPIC.machineFileInfo.subscribeTopic}${this.deviceId}`)
      }
    },
    // 订阅并发送消息,订阅主题是外部存储发送结果
    onSubscribe2 (programId, onMessageArrived) {
      if (this.programId1.length === 0 || this.programId1 !== programId) {
        this.programId1 = programId
        // 订阅主题
        this.client.subscribe(
          `${this.CommonConfig.socketConfig.SOCKET_TOPIC.machineFileSendResult.subscribeTopic}${this.programId1}`,
          onMessageArrived,
          { id: `${this.CommonConfig.socketConfig.SOCKET_TOPIC.machineFileSendResult.subscribeTopic}${this.programId1}` }
        )
        this.socketList.push(`${this.CommonConfig.socketConfig.SOCKET_TOPIC.machineFileSendResult.subscribeTopic}${this.programId1}`)
      }
    },
    onSubscribeNewBox (cmd, onMessageArrived) {
      this.client.subscribe(
        `${this.CommonConfig.socketConfig.SOCKET_TOPIC.machineFileSendResultNew.subscribeTopic}${cmd}`,
        onMessageArrived,
        { id: `${this.CommonConfig.socketConfig.SOCKET_TOPIC.machineFileSendResultNew.subscribeTopic}${cmd}` }
      )
      this.socketList.push(`${this.CommonConfig.socketConfig.SOCKET_TOPIC.machineFileSendResultNew.subscribeTopic}${cmd}`)
    },
    onUnSubscribeNewBox (cmd) {
      this.client.unsubscribe(`${this.CommonConfig.socketConfig.SOCKET_TOPIC.machineFileSendResultNew.subscribeTopic}${cmd}`)
    },
    onUnSubscribeBox (cmd) {
      this.client.unsubscribe(`${this.CommonConfig.socketConfig.SOCKET_TOPIC.machineFileInfo.subscribeTopic}${cmd}`)
    },
    // 取消文件传输结果的订阅
    onUnSubscribeResultBox (cmd) {
      this.client.unsubscribe(`${this.CommonConfig.socketConfig.SOCKET_TOPIC.machineFileSendResult.subscribeTopic}${cmd}`)
    }
  }
}
